Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
event-emitter
Advanced tools
The event-emitter npm package is a lightweight and flexible library for managing events in JavaScript. It allows you to create, listen to, and emit events, making it easier to handle asynchronous operations and decouple different parts of your application.
Creating an Event Emitter
This feature allows you to create a new event emitter instance. The instance can be used to manage events within your application.
const EventEmitter = require('event-emitter');
const emitter = EventEmitter();
Listening to Events
This feature allows you to listen for specific events. When the event is emitted, the provided callback function is executed.
emitter.on('event', function(data) {
console.log('Event received:', data);
});
Emitting Events
This feature allows you to emit events. When an event is emitted, all listeners for that event are called with the provided data.
emitter.emit('event', { key: 'value' });
Removing Event Listeners
This feature allows you to remove specific event listeners. This is useful for cleaning up and preventing memory leaks.
const callback = function(data) {
console.log('Event received:', data);
};
emitter.on('event', callback);
emitter.off('event', callback);
The 'events' package is a core Node.js module that provides a similar event-driven architecture. It is more robust and feature-rich compared to event-emitter, offering additional functionalities like once listeners and error handling.
The 'eventemitter3' package is a high-performance event emitter for Node.js and the browser. It is similar to event-emitter but is optimized for speed and has a smaller footprint.
The 'mitt' package is a tiny, functional event emitter. It is simpler and more lightweight compared to event-emitter, making it ideal for small projects or performance-critical applications.
$ npm install event-emitter
To port it to Browser or any other (non CJS) environment, use your favorite CJS bundler. No favorite yet? Try: Browserify, Webmake or Webpack
var ee = require('event-emitter');
var MyClass = function () { /* .. */ };
ee(MyClass.prototype); // All instances of MyClass will expose event-emitter interface
var emitter = new MyClass(), listener;
emitter.on('test', listener = function (args) {
// … react to 'test' event
});
emitter.once('test', function (args) {
// … react to first 'test' event (invoked only once!)
});
emitter.emit('test', arg1, arg2/*…args*/); // Two above listeners invoked
emitter.emit('test', arg1, arg2/*…args*/); // Only first listener invoked
emitter.off('test', listener); // Removed first listener
emitter.emit('test', arg1, arg2/*…args*/); // No listeners invoked
Removes all listeners from given event emitter object
Whether object has some listeners attached to the object.
When name
is provided, it checks listeners for specific event name
var emitter = ee();
var hasListeners = require('event-emitter/has-listeners');
var listener = function () {};
hasListeners(emitter); // false
emitter.on('foo', listener);
hasListeners(emitter); // true
hasListeners(emitter, 'foo'); // true
hasListeners(emitter, 'bar'); // false
emitter.off('foo', listener);
hasListeners(emitter, 'foo'); // false
Pipes all events from source emitter onto target emitter (all events from source emitter will be emitted also on target emitter, but not other way).
Returns pipe object which exposes pipe.close
function. Invoke it to close configured pipe.
It works internally by redefinition of emit
method, if in your interface this method is referenced differently, provide its name (or symbol) with third argument.
Unifies event handling for two objects. Events emitted on emitter1 would be also emitted on emitter2, and other way back.
Non reversible.
var eeUnify = require('event-emitter/unify');
var emitter1 = ee(), listener1, listener3;
var emitter2 = ee(), listener2, listener4;
emitter1.on('test', listener1 = function () { });
emitter2.on('test', listener2 = function () { });
emitter1.emit('test'); // Invoked listener1
emitter2.emit('test'); // Invoked listener2
var unify = eeUnify(emitter1, emitter2);
emitter1.emit('test'); // Invoked listener1 and listener2
emitter2.emit('test'); // Invoked listener1 and listener2
emitter1.on('test', listener3 = function () { });
emitter2.on('test', listener4 = function () { });
emitter1.emit('test'); // Invoked listener1, listener2, listener3 and listener4
emitter2.emit('test'); // Invoked listener1, listener2, listener3 and listener4
$ npm test
FAQs
Environment agnostic event emitter
The npm package event-emitter receives a total of 6,238,069 weekly downloads. As such, event-emitter popularity was classified as popular.
We found that event-emitter demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.